// src/store/customerServiceSlice.js

import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import { fetchUnreadMessageCount as fetchUnreadMessageCountAPI } from '../api/customerService';


// 创建异步 thunk 来获取未读消息数
export const fetchUnreadMessageCount = createAsyncThunk(
  'customerService/fetchUnreadMessageCount',
  async () => {
    const response = await fetchUnreadMessageCountAPI();
    // 确保返回的是一个数字
    return typeof response.data === 'number' ? response.data : 0;
  }
);

// 创建客服服务的 slice
const customerServiceSlice = createSlice({
  name: 'customerService',
  initialState: {
    unreadMessageCount: 0,
  },
  reducers: {},
  extraReducers: (builder) => {
    builder.addCase(fetchUnreadMessageCount.fulfilled, (state, action) => {
      state.unreadMessageCount = action.payload;
    });
  },
});

export default customerServiceSlice.reducer;